import paramiko
from datetime import datetime
def scp_by_key(host_ip, host_port, remote_path, local_path, username, pkey_path):
  try:
    key=paramiko.RSAKey.from_private_key_file(pkey_path)
    t = paramiko.Transport((host_ip, host_port))
    t.connect(username=username, pkey=key)
    sftp = paramiko.SFTPClient.from_transport(t)
    src = remote_path
    des = local_path
    sftp.get(src,des)
    t.close()
  except Exception as e:
    print(111,e)

filename = 'yzd_server_%s.xlsx' % datetime.now().__format__('%m-%d')
scp_by_key('172.16.44.177', 22,'/home/logger/%s' % filename,'/tmp/%s' % filename,'logger','/root/.ssh/id_rsa_172.16.44.177')

#密码，则只需将pkey参数换为password传入即可
#t = paramiko.Transport((host_ip, host_port))
#t.connect(username=username, password='xxx')
